#include "loginwgt.h"
#include <QMouseEvent>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPainter>
#include <QAction>
#include "UI/tool/styleloader.h"
#include "ICMS.h"
#include "appconfig.h"

LoginWgt::LoginWgt(QWidget *parent)
    : QWidget(parent)
    ,m_bDrag(false)//初始化拖拽状态
{
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_StyledBackground);
    setFixedSize(300,400);
    setObjectName("mainWidget"); // 设置主窗口对象名
    initLayout();
    StyleLoader::getInstance()->loadStyle(":/qss/main.css",this);
}

void LoginWgt::slo_Operator(const REQUESTTYPE type, bool result)
{
    switch (type) {
    case REGISTER:
        handleRegister(result);
        break;
    case LOGIN:
        handleLogin(result);
        break;
    default:
        break;
    }
}

void LoginWgt::slo_MediaInfo(const QVector<QVariantMap> mediainfo)
{
    //
     if(!mediainfo.isEmpty())
     {
         ICMS* icms=new ICMS();
         icms->show();
         hide();
     }
}

void LoginWgt::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        if(event->y()<30){
            m_bDrag = true;//设置拖拽状态为真
            m_dragStartPos = event->pos();//相对坐标
            m_dragGlobalPos= event->globalPos();//全局坐标
            setCursor(Qt::ClosedHandCursor);//设置抓取光标
            event->accept();
        }
    }

}

void LoginWgt::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bDrag&&(event->buttons()&Qt::LeftButton)){
        //计算新位置 = 当前鼠标位置-初始偏移量
        QPoint newPos = event->globalPos() - m_dragStartPos;
        move(newPos);//移动窗口
        event->accept();
    }

}

void LoginWgt::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        m_bDrag = false;
        setCursor(Qt::ArrowCursor);//恢复默认光标
        event->accept();
    }

}

void LoginWgt::handleLogin(bool result)
{
     if(result)
     {
         AppConfig* app=AppConfig::instance();
         HttpHelper* httpHelper=HttpHelper::instance();
         QVector<QVariantMap>imfas=app->getVideoFiles();
         for(const QVariantMap& video : imfas)
         {
             if(video.contains("mediaUrl")&& video.contains("mediaTitle"))
             {
                 QString mediaUrl=video["mediaUrl"].toString();
                 QString mediaTitle=video["mediaTitle"].toString();
                 httpHelper->slot_AddmediaInfo(video);
                 qDebug()<<"媒体文件:"<<mediaTitle<<"路径:"<<mediaUrl;
             }
             else
             {
                 qDebug()<<"缺少必要手段";
             }
         }
         httpHelper->slot_GetmediaInfo();
     }
     else
     {
         qDebug()<<"登录失败";
     }
}

void LoginWgt::handleRegister(bool result)
{
    if(result)
    {
        stackedWgt->setCurrentWidget(loginWgt);
    }
    else
    {
        qDebug()<<"注册失败";
    }
}

void LoginWgt::initLayout()
{
    //主布局
    QVBoxLayout* mainlyout = new QVBoxLayout;
    //创建公共窗口（标题窗口，用于关闭程序）
    QWidget* headrWidge = new QWidget(this);
    headrWidge->setFixedSize(300,150);
    headrWidge->setWindowFlag(Qt::FramelessWindowHint);
    headrWidge->setAttribute(Qt::WA_StyledBackground);

    //    //登录用户头像
    //    QLabel* iconlLabel = new QLabel(headrWidge);
    //    iconlLabel->setAlignment(Qt::AlignCenter);
    //    iconlLabel->setFixedSize(80,80);
    QPushButton* closeBtn = new QPushButton(headrWidge);

    closeBtn->setFixedSize(30,30);
    closeBtn->setObjectName("closeBtn");

    //    //头像设置图片
    //    iconlLabel->setPixmap(QPixmap(":/qss/icon/logo.png"));//图片路径
    //    //图片缩放
    //    iconlLabel->setScaledContents(true);

    // 创建头像标签
    QLabel* avatarLabel = new QLabel();

    // 加载原始图片
    QPixmap avatar(":/qss/icon/logo.png"); // 替换为你的图片路径

    // 创建圆形头像
    QPixmap roundedAvatar(80, 80);
    roundedAvatar.fill(Qt::transparent);
    QPainter painter(&roundedAvatar);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(QBrush(avatar.scaled(80, 80, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation)));
    painter.setPen(Qt::NoPen);
    painter.drawEllipse(0, 0, 80, 80);
    avatarLabel->setPixmap(roundedAvatar);
    avatarLabel->setFixedSize(80, 80);
    avatarLabel->setAlignment(Qt::AlignCenter);



    //关闭按钮绑定信号与槽
    connect(closeBtn,&QPushButton::clicked,this,[this](){
        close();
    });
    //headerWidge布局
    QVBoxLayout* headerLayout = new QVBoxLayout(headrWidge);
    headerLayout->setContentsMargins(0,0,0,0);
    headerLayout->setSpacing(0);
    //顶部水平布局
    QHBoxLayout* toplayout = new QHBoxLayout();
    toplayout->setContentsMargins(0,0,0,0);
    toplayout->addStretch();
    toplayout->addWidget(closeBtn);
    headerLayout->addLayout(toplayout);

    //左键间隔
    headerLayout->addStretch();

    //图标布局
    QHBoxLayout* iconlayout = new QHBoxLayout();
    iconlayout->setContentsMargins(0,0,0,0);
    iconlayout->addStretch();//弹簧
    iconlayout->addWidget(avatarLabel);
    iconlayout->setSpacing(10);
    iconlayout->addStretch();//弹簧
    headerLayout->addLayout(iconlayout);
    headrWidge->setLayout(headerLayout);

    //stackedWidge
    stackedWgt = new QStackedWidget(this);
    stackedWgt->setFixedSize(300,250);
    stackedWgt->setWindowFlag(Qt::FramelessWindowHint);
    stackedWgt->setAttribute(Qt::WA_StyledBackground);

    mainlyout->setSpacing(0);
    mainlyout->addWidget(headrWidge);
    mainlyout->addWidget(stackedWgt);
    mainlyout->setContentsMargins(0,0,0,0);

    //登录窗口
    loginWgt = new QWidget(stackedWgt);
    registerWgt = new QWidget(stackedWgt);

    loginWgt->setFixedSize(300,250);
    loginWgt->setWindowFlag(Qt::FramelessWindowHint);
    loginWgt->setAttribute(Qt::WA_StyledBackground);


    registerWgt->setFixedSize(300,250);
    registerWgt->setWindowFlag(Qt::FramelessWindowHint);
    registerWgt->setAttribute(Qt::WA_StyledBackground);

    //添加登录窗口子控件
    QLineEdit* account = new QLineEdit(loginWgt);//创建用户名输入框
    QLineEdit* password = new QLineEdit(loginWgt);//创建密码输入框
    QPushButton* loginBtn = new QPushButton("登录",loginWgt);//创建登录按钮
    QPushButton* registerBtn = new QPushButton("没有账号，请先注册",registerWgt);//创建注册按钮
    QAction *accountIcon = new QAction(account);//创建账号图标
    QAction *passwordIcon = new QAction(password);//创建密码图标

    loginBtn->setObjectName("loginButton");
    registerBtn->setObjectName("registerBtn");
    account->setObjectName("loginAccount");
    accountIcon->setIcon(QIcon(":/qss/icon/user.png"));
    account->addAction(accountIcon, QLineEdit::LeadingPosition);
    password->setObjectName("loginPassword");
    passwordIcon->setIcon(QIcon(":/qss/icon/password.png"));
    password->addAction(passwordIcon, QLineEdit::LeadingPosition);


    account->setFixedSize(250,40);
    password->setFixedSize(250,40);
    loginBtn->setFixedSize(250,40);
    registerBtn->setFixedSize(250,40);


    account->setPlaceholderText("请输入用户名");
    password->setPlaceholderText("请输入密码");
    //隐藏的密码格式
    password->setEchoMode(QLineEdit::Password);
    //登录窗口子控件布局
    QVBoxLayout* loginlayout = new QVBoxLayout(loginWgt);
    loginlayout->setAlignment(Qt::AlignHCenter);
    loginlayout->setSpacing(10);
    loginlayout->addWidget(account);
    loginlayout->addWidget(password);
    loginlayout->addStretch();

    loginlayout->addWidget(loginBtn);
    loginlayout->addWidget(registerBtn);
    loginlayout->addStretch();
    loginlayout->setContentsMargins(0,0,0,0);
    loginWgt->setLayout(loginlayout);

    //注册子控件
    QLineEdit* reusername = new QLineEdit(registerWgt);
    QLineEdit* repassword = new QLineEdit(registerWgt);
    QPushButton* starRegisterBtn = new QPushButton("开始注册",registerWgt);
    QPushButton* reloginBtn = new QPushButton("返回登录",registerWgt);
    QAction *accounticon = new QAction(reusername);//创建账号图标
    QAction *passwordicon = new QAction(repassword);//创建密码图标

    reloginBtn->setObjectName("loginButton");
    starRegisterBtn->setObjectName("registerBtn");

    reusername->setObjectName("loginAccount");
    accounticon->setIcon(QIcon(":/qss/icon/user.png"));
    reusername->addAction(accounticon, QLineEdit::LeadingPosition);

    repassword->setObjectName("loginPassword");
    passwordicon->setIcon(QIcon(":/qss/icon/password.png"));
    repassword->addAction(passwordicon, QLineEdit::LeadingPosition);


    reloginBtn->setObjectName("loginButton");
    starRegisterBtn->setObjectName("registerBtn");

    connect(loginBtn,&QPushButton::clicked,this,[=](){
       QVariantMap infoMap;
       QString username=account->text();
       QString userpasswd=password->text();
       if(!username.isEmpty()&& !userpasswd.isEmpty())
       {
           infoMap["uderName"]=username;
           infoMap["password"]=userpasswd;
           infoMap["nickName"]="text";
           HttpHelper::instance()->slot_Login(infoMap);
       }
       else
       {
           qDebug()<<"账号密码不能为空";
       }
    });

    connect(starRegisterBtn,&QPushButton::clicked,this,[=](){
       QVariantMap infoMap;
       QString reusername=account->text();
       QString reuserpasswd=password->text();
       if(!reusername.isEmpty()&& !reuserpasswd.isEmpty())
       {
           infoMap["uderName"]=reusername;
           infoMap["password"]=reuserpasswd;
           infoMap["nickName"]="text";
           HttpHelper::instance()->slot_Login(infoMap);
       }
       else
       {
           qDebug()<<"账号密码不能为空";
       }
    });

    connect(reloginBtn,&QPushButton::clicked,this,[this](){
        stackedWgt->setCurrentWidget(loginWgt);
    });

    //设置注册窗口子控件大小
    reusername->setFixedSize(250,40);
    repassword->setFixedSize(250,40);
    starRegisterBtn->setFixedSize(250,40);
    reloginBtn->setFixedSize(250,40);

    //编辑框设置占位符
    reusername->setPlaceholderText("请输入用户名");
    repassword->setPlaceholderText("请输入密码");
    repassword->setEchoMode(QLineEdit::Password);

    //注册窗口设置布局
    QVBoxLayout* registerlayout = new QVBoxLayout(registerWgt);
    registerlayout->setAlignment(Qt::AlignHCenter);
    registerlayout->setSpacing(10);
    registerlayout->addWidget(reusername);
    registerlayout->addWidget(repassword);
    registerlayout->addStretch();
    registerlayout->addWidget(starRegisterBtn);
    registerlayout->addWidget(reloginBtn);
    registerlayout->addStretch();
    registerlayout->setContentsMargins(0,0,0,0);
    registerWgt->setLayout(registerlayout);

    //stackedwidget添加子窗口
    stackedWgt->addWidget(loginWgt);
    stackedWgt->addWidget(registerWgt);

    //注册按钮绑定槽
    connect(registerBtn,&QPushButton::clicked,this,[this](){
        stackedWgt->setCurrentWidget(registerWgt);
    });

    stackedWgt->setCurrentWidget(loginWgt);

    this->setLayout(mainlyout);
}
